//package com.liqing.movecomment.util;
//
//import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.liqing.movecomment.elastic.entity.MoveElas;
//import org.elasticsearch.action.bulk.BulkRequest;
//import org.elasticsearch.action.bulk.BulkResponse;
//import org.elasticsearch.action.delete.DeleteRequest;
//import org.elasticsearch.action.delete.DeleteResponse;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.action.update.UpdateRequest;
//import org.elasticsearch.action.update.UpdateResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.springframework.stereotype.Component;
//import static org.elasticsearch.client.RequestOptions.DEFAULT;
//import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.action.support.master.AcknowledgedResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.client.indices.CreateIndexRequest;
//import org.elasticsearch.client.indices.CreateIndexResponse;
//import org.elasticsearch.client.indices.GetIndexRequest;
//import org.elasticsearch.common.settings.Settings;
//import org.elasticsearch.common.xcontent.XContentBuilder;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.elasticsearch.common.xcontent.json.JsonXContent;
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.util.*;
//import javax.annotation.Resource;
//import java.io.IOException;
//
///**
// * @author 黄靖
// * @author 2023-05-22 12:48
// * description:
// */
//@Component
//public class ESUtils {
//    //<editor-fold defaultstate="collapsed" desc="delombok">
//    @SuppressWarnings("all")
//    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ESUtils.class);
//    //</editor-fold>
//    private static final String INDEX = "move-index";
//    @Resource
//    RestHighLevelClient client;
//    //<editor-fold defaultstate="collapsed" desc="delombok">
//    public static final ESUtils INSTENCE = new ESUtils();
//    //</editor-fold>
//
//    /*
//    * 根据name all——match查询
//    * 返回 ids
//    * */
//    public static List<Integer> searchByName(String keyWord) throws IOException {
//        // 1、创建查询请求的对象
//        SearchRequest searchRequest = new SearchRequest(INDEX);
//        // 2、查询条件构造器
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        // 3、设置查询条件
//        // ------------
//        builder.query(QueryBuilders.matchQuery("name", keyWord));
//        // ------------
//        log.info("执行的查询是==》{}", keyWord);
//        searchRequest.source(builder);
//        // 4、执行查询
//        SearchResponse response = INSTENCE.client.search(searchRequest, RequestOptions.DEFAULT);
//        // 5、获取查询结果
//        SearchHit[] hits = response.getHits().getHits();
//        List<Integer> list = new ArrayList<>();
//        for (SearchHit hit : hits) {
//            Map<String, Object> map = hit.getSourceAsMap();
//            Integer id = (Integer) map.get("id");
//            log.info("{}", map);
//            list.add(id);
//        }
//        return list;
//    }
//
//    /*
//    根据id删除
//    * */
//    public static void deleteDocument(String id) throws IOException {
//        // 构造删除索引请求 指定索引名 数据ID
//        DeleteRequest delete = new DeleteRequest(INDEX, id);
//        DeleteResponse response = INSTENCE.client.delete(delete, RequestOptions.DEFAULT);
//        System.out.println(response);
//    }
//
//    /*
//    * 新增 数据
//    * */
//    public static void insertDocument(Integer id, String name) {
//        MoveElas moveElas = new MoveElas(id, name);
//        com.fasterxml.jackson.databind.ObjectMapper mapper = new ObjectMapper();
//        BulkRequest request = new BulkRequest();
//        try {
//            request.add(new IndexRequest(INDEX).id(moveElas.getId().toString()).source(mapper.writeValueAsString(moveElas), XContentType.JSON));
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
//        BulkResponse response = null;
//        try {
//            response = INSTENCE.client.bulk(request, DEFAULT);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        if (ObjectUtils.isNotEmpty(response)) {
//            log.info("数据插入成功");
//        }
//    }
//
//    public static void updateDocumentMap(Integer id, String name) {
//        // 更新哪个索引的哪条数据
//        UpdateRequest update = new UpdateRequest(INDEX, id.toString());
//        // 更新的内容
//        Map<String, Object> map = new HashMap<>();
//        map.put("name", name);
//        update.doc(map);
//        // 发送更新请求
//        UpdateResponse response = null;
//        try {
//            response = INSTENCE.client.update(update, RequestOptions.DEFAULT);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        if (ObjectUtils.isNotEmpty(response)) {
//            log.info("修改成功");
//        }
//    }
//}
